package com.greenline.search.test.utils;

import com.greenline.search.test.dao.AnalyzeObject;
import com.greenline.search.test.httprequest.MyHttpRequest;
import com.greenline.search.test.httprequest.impl.MyHttpClientRequest;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: liulei
 * Date: 13-12-18
 * Time: 下午5:49
 * To change this template use File | Settings | File Templates.
 */
//未完成
public class AnalyzeJSONToObject {

    private Logger logger = LoggerFactory.getLogger(AnalyzeJSONToObject.class);
    private MyHttpRequest httpRequest = new MyHttpClientRequest();
    //default analyze url
    private String analyzeURL = "http://10.10.100.153:8090/analyze/?q=";

    public List<AnalyzeObject> getAnalyzeObjectListFromJson(String keyword, String analyzeURL){
        List<AnalyzeObject> analyzeObjectList = new ArrayList<AnalyzeObject>();
        if(analyzeURL == null || analyzeURL.isEmpty()){
            analyzeURL = this.analyzeURL;
        }
        try {
            analyzeURL = analyzeURL + URLEncoder.encode(keyword,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            logger.error("Exception:"+e.getMessage());
        }
        byte[] responseBody =  httpRequest.request(analyzeURL);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            analyzeObjectList.add(objectMapper.readValue(responseBody,AnalyzeObject.class));
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return analyzeObjectList;
    }

    public static void main(String[] args) {
        AnalyzeJSONToObject object = new AnalyzeJSONToObject();
        System.out.println(object.getAnalyzeObjectListFromJson("傅雯雯",null).get(0).getKey());
    }
}
